<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class MemberdrawGroupExport implements FromQuery, WithMapping, WithHeadings
{
    use Exportable;

    protected  $model;

    public function __construct($model)
    {
        $this->model = $model;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
        return $this->model->query()->where(function($query){
            $query->where('pid',request()->id);
            $query->with(['roomsGoods' => function($buys){
                $buys->with('room_goods');
                return $buys;
            }]);
             if(request()->keyword)
            {
                $query->where('mobile','like','%'.request()->keyword.'%');
            }
            if(isset(request()->time[0]) && !empty(request()->time[0])){
                $query->whereBetween('created_at', request()->time);
            }
        });
    }

    public function map($invoice): array
    {
        return [
            $invoice->nickname,
            $invoice->mobile,
            $invoice->bean,
            $invoice->room_num,
            $invoice->created_at,
        ];
    }

    public function headings(): array
    {
        return [
            '会员昵称',
            '手机号',
            '金豆数量',
            '所在房间号',
            '交易时间',
        ];
    }
}
